package com.example.javamaday.offer;

import java.util.Comparator;
import java.util.PriorityQueue;

public class GetOffer40 {

    public static void main(String[] args) {

    }

    public int[] getLeastNumbers(int[] arr, int k) {
        if (k == 0) {
            return new int[0];
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer t1) {
                return 0 - integer.compareTo(t1);
            }
        });
        for (int j : arr) {
            priorityQueue.offer(j);
            if (priorityQueue.size() > k) {
                priorityQueue.poll();
            }
        }
        int length = priorityQueue.size();
        int[] result = new int[length];
        for (int i = 0; i < length; i++) {
            result[i] = priorityQueue.poll();
        }
        return result;
    }
}
